﻿// ResourceReader.cs created with MonoDevelop
// User: armando at 16:10 25/01/2009
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//

using System;
using System.Xml;
using System.IO;
using System.Text;


namespace ABasile
{
	
	
	public class ResourceReader
	{

		XmlDocument MyResourceDoc;
		FileInfo MyResourceFile;
		
		public ResourceReader(string ResourceFolder, 
		                      string ResourceBaseName, 
		                      string ResourceCulture)
		{
			// Create Paths
			string CommonResource = ResourceFolder.Trim();
			string TargetResource = "";
			
			if (CommonResource.Substring(CommonResource.Length-1) != Path.DirectorySeparatorChar.ToString() )
				CommonResource += Path.DirectorySeparatorChar;

			CommonResource += ResourceBaseName + ".";
			TargetResource = CommonResource;

			CommonResource += "xml";
			TargetResource += ResourceCulture + ".xml";
			
			// Check TargetResource and CommonResource file exist
			MyResourceFile = new FileInfo(TargetResource);
			if (!MyResourceFile.Exists)
			{
				MyResourceFile = new FileInfo(CommonResource);
				if (!MyResourceFile.Exists)
				{
					throw new ArgumentException("Missing common and custom culture resource files.");
				}
			}

			// Load Resource file
			MyResourceDoc = new XmlDocument();

			try
			{
				MyResourceDoc.Load(MyResourceFile.FullName);
				
				
			}
			catch (Exception Ex)
			{
				throw Ex;
			}
			
		}

		public string GetString(string KeyName)
		{
			try
			{
				XmlNode MyResourceNode = MyResourceDoc.SelectSingleNode("/root/" + KeyName);
				return MyResourceNode.InnerXml;
			}
			catch (Exception Ex)
			{
				// ABasile.Utility Uman = new ABasile.Utility();
				// Uman.AppendTraceFile ("ResourceReader", "GetString", "KeyName=" + KeyName);
				throw Ex;
			}			
		}


		
	}
}

